package com.qezhhnjy.flink.job;

import akka.stream.javadsl.Sink;
import com.qezhhnjy.flink.mapper.MyFlatMapper;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

/**
 * @author zhaoyangfu
 * @date 2021/8/24-16:23
 */
public class StreamKafka {

    public static void stream(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // kafka 配置项
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "192.168.16.50:9092,192.168.16.51:9092,192.168.16.52:9092");
        properties.setProperty("group.id", "consumer-group");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("auto.offset.reset", "latest");
        // 从 kafka 读取数据
        DataStream<String> dataStream = env.addSource(new FlinkKafkaConsumer<>("test", new SimpleStringSchema(), properties));
        dataStream.flatMap(new MyFlatMapper()).keyBy((KeySelector<Tuple2<String, Integer>, String>) value -> value.f0).sum(1).print();
        env.execute();
    }
}
